package com.stylefeng.guns.modular.wz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.persistence.dao.PfFileMapper;
import com.stylefeng.guns.common.persistence.dao.WzUserInfoMapper;
import com.stylefeng.guns.common.persistence.model.*;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.wz.dao.WzCouponDao;
import com.stylefeng.guns.modular.wz.util.DateUtil;
import com.stylefeng.guns.modular.wz.warpper.WzCouponWarpper;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.wz.service.IWzCouponService;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.stylefeng.guns.modular.wz.util.DateUtil.format_ymdhms;

/**
 * 优惠券控制器
 *
 * @author fengshuonan
 * @Date 2018-03-28 09:12:16
 */
@Controller
@RequestMapping("/wzCoupon")
public class WzCouponController extends BaseController {

    private String PREFIX = "/wz/wzCoupon/";

    @Autowired
    private IWzCouponService wzCouponService;

    @Autowired
    private WzCouponDao wzCouponDao;
    @Autowired
    private WzUserInfoMapper userInfoMapper;
    @Autowired
    private PfFileMapper fileMapper;

    /**
     * 跳转到优惠券首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "wzCoupon.html";
    }

    /**
     * 跳转到优惠券首页
     */
    @RequestMapping("/index")
    public String list() {
        return PREFIX + "coupon.html";
    }

    /**
     * 跳转到添加优惠券
     */
    @RequestMapping("/wzCoupon_add")
    public String wzCouponAdd(Integer type, Model model) {
        model.addAttribute("type", type);
        return PREFIX + "wzCoupon_add.html";
    }

    /**
     * 跳转到修改优惠券
     */
    @RequestMapping("/wzCoupon_update/{wzCouponId}")
    public String wzCouponUpdate(@PathVariable Long wzCouponId, Model model) {
        WzCoupon wzCoupon = wzCouponService.selectById(wzCouponId);
        model.addAttribute("item", wzCoupon);
        model.addAttribute("beginTime", DateUtil.toString(wzCoupon.getBeginTime(), format_ymdhms));
        model.addAttribute("endTime", DateUtil.toString(wzCoupon.getEndTime(), format_ymdhms));
        model.addAttribute("type", ConstantFactory.me().getDictsByName("奖券类型", wzCoupon.getType()));
        model.addAttribute("subtype", ConstantFactory.me().getDictsByName("奖券子类型", wzCoupon.getType()));
        model.addAttribute("fileId", "");
        List<PfFile> files = fileMapper.selectList(new EntityWrapper<PfFile>().eq("file_url", wzCoupon.getCoverUrl()));
        if (files.size() > 0) {
            model.addAttribute("fileId", files.get(0).getFileId());
        }
        WzUserInfo userInfo = userInfoMapper.selectById(wzCoupon.getUserInfoId());
        model.addAttribute("merchantTel", userInfo.getMerchantTel());
        model.addAttribute("merchantAddress", userInfo.getMerchantAddress());
        LogObjectHolder.me().set(wzCoupon);
        return PREFIX + "wzCoupon_edit.html";
    }

    /**
     * 获取优惠券列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition, Integer type, Integer status, String userCode) {
        List<Map<String, Object>> wzCouponList = wzCouponDao.selectWzCoupons(null, condition, type, status, userCode);
        return new WzCouponWarpper(wzCouponList).warp();
    }

    /**
     * 新增优惠券
     */
    @Transactional
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(WzCoupon wzCoupon, String userInfoId, String userCode, String fileId) {
        PfFile file = fileMapper.selectById(fileId);
        if (file != null) {
            wzCoupon.setCoverUrl(file.getFileUrl());
        } else {
            return super.SUCCESS_TIP.fail("封面图片不存在!");
        }
        wzCoupon.setUserInfoId(Long.parseLong(userInfoId));
        wzCoupon.setUserCode(userCode);
        wzCoupon.setModifierId(ShiroKit.getUser().getId());
        wzCoupon.setCreateTime(new Date());
        wzCouponService.insert(wzCoupon);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除优惠券
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long wzCouponId) {
        WzCoupon coupon = wzCouponService.selectById(wzCouponId);
        if (coupon.getStatus() == 1) {
            wzCouponService.deleteById(wzCouponId);
            return SUCCESS_TIP;
        } else {
            return SUCCESS_TIP.fail("只有待使用奖券才可删除!");
        }
    }

    /**
     * 修改优惠券
     */
    @Transactional
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(WzCoupon wzCoupon, String userInfoId, String userCode, String fileId) {
        PfFile file = fileMapper.selectById(fileId);
        if (file != null) {
            wzCoupon.setCoverUrl(file.getFileUrl());
        } else {
            return super.SUCCESS_TIP.fail("封面图片不存在!");
        }
        if (StringUtils.isNotBlank(userInfoId)) {
            wzCoupon.setUserInfoId(Long.parseLong(userInfoId));
        }
        if (StringUtils.isNotBlank(userCode)) {
            wzCoupon.setUserCode(userCode);
        }
        wzCoupon.setModifierId(ShiroKit.getUser().getId());
        wzCoupon.setUpdateTime(new Date());
        wzCouponService.updateById(wzCoupon);
        return super.SUCCESS_TIP;
    }

    /**
     * 优惠券详情
     */
    @RequestMapping(value = "/detail/{wzCouponId}")
    @ResponseBody
    public Object detail(@PathVariable("wzCouponId") Long wzCouponId) {
        return wzCouponService.selectById(wzCouponId);
    }
}
